﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CentralOffice.Archive.Data;
using CentralOffice.Archive.BusinessActions;

using PublicDatumTable = CentralOffice.Archive.Data.PublicDatumDataSet.PublicDatumDataTable;
using PublicDatumRow = CentralOffice.Archive.Data.PublicDatumDataSet.PublicDatumRow;
using System.Data;


namespace CentralOffice.WebClient.Archives
{
    public partial class PublicDatumQuery : PageBase
    {
        private bool? authorized = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                SiteUtil.PopulateListBox(ddlCategory, SiteConstants.ArchiveType, true);
                ddlCategory.SelectedIndex = 0;

                if (authorized == null)
                {
                    authorized = this.Authorize(AuthorizationRules.PublicDatum_Edit);
                }
                if (!authorized.Value)
                {
                    gv.Columns.RemoveAt(gv.Columns.Count - 1);
                }
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            gv.PageIndex = 0;

            PublicDatumRow publicDatum = (new PublicDatumDataSet()).PublicDatum.NewPublicDatumRow();

            
            publicDatum.Name = txtName.Text;
            //publicDatum.Category = txtCategory.Text;
            //publicDatum.Remark = txtRemark.Text;
            if (ddlCategory.SelectedIndex > 0)
            {
                publicDatum.Category = Int32.Parse(ddlCategory.SelectedItem.Value);
            }
            

            //publicDatum.Attachments = txtAttachments.Text;
            

            odsPublicDatum.SelectParameters.Clear();
            odsPublicDatum.SelectParameters.Add("whereClause", publicDatum.FilterExpression);
        }

        protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                PublicDatumRow train = ((DataRowView)e.Row.DataItem).Row as PublicDatumRow;

                if (!train.IsAttachmentsNull())
                {
                    HyperLink link = e.Row.Cells[4].Controls[0] as HyperLink;
                    SiteUtil.GenernateDownloadHyperLink(link, train.Attachments);
                }
                
            }
        }
    }
}
